package com.kang.controller;

import com.kang.aop.operLog.OperLog;
import com.kang.entity.SysMenu;
import com.kang.menu.BusinessType;
import com.kang.service.SysMenuService;
import com.kang.util.WebResponse;
import com.kang.vo.AssginMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
@Slf4j
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("菜单列表")
    @GetMapping("findNodes")
    public WebResponse findNodes() {
        try {
            return WebResponse.resSuccess("菜单列表查询成功",sysMenuService.findNodes());
        } catch (Exception ex) {
            log.info("菜单列表查询成功",ex);
            return WebResponse.resFail("菜单列表查询失败",null);
        }
    }

    @OperLog(title = "添加菜单", businessType = BusinessType.INSERT)
    @ApiOperation("添加菜单")
    @PostMapping("saveMenu")
    public WebResponse saveMenu(@RequestBody SysMenu sysMenu) {
        try {
            return WebResponse.resSuccess("添加菜单成功",sysMenuService.saveMenu(sysMenu));
        } catch (Exception ex) {
            log.info("添加菜单失败",ex);
            return WebResponse.resFail("添加菜单失败",null);
        }
    }

    @ApiOperation("根据ID查询菜单")
    @GetMapping("findNode/{id}")
    public WebResponse findNodeById(@PathVariable Integer id) {
        try {
            return WebResponse.resSuccess("根据ID查询菜单成功",sysMenuService.findNodeById(id));
        } catch (Exception ex) {
            log.info("根据ID查询菜单失败",ex);
            return WebResponse.resFail("根据ID查询菜单失败",null);
        }
    }

    @OperLog(title = "修改菜单", businessType = BusinessType.UPDATE)
    @ApiOperation("修改菜单")
    @PostMapping("updateMenu")
    public WebResponse updateMenu(@RequestBody SysMenu sysMenu) {
        try {
            return WebResponse.resSuccess("修改菜单成功",sysMenuService.updateMenu(sysMenu));
        } catch (Exception ex) {
            log.info("修改菜单失败",ex);
            return WebResponse.resFail("修改菜单失败",null);
        }
    }

    @OperLog(title = "逻辑删除角色", businessType = BusinessType.DELETE)
    @ApiOperation("删除菜单")
    @DeleteMapping("deleteMenuById/{id}")
    public WebResponse deleteMenuById(@PathVariable Integer id) {
        try {
            return WebResponse.resSuccess("删除菜单成功",sysMenuService.deleteMenuById(id));
        } catch (Exception ex) {
            log.info("删除菜单失败",ex);
            return WebResponse.resFail("删除菜单失败",null);
        }
    }

    @ApiOperation("根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public WebResponse toAssign(@PathVariable Integer roleId) {
        try {
            return WebResponse.resSuccess("根据角色获取菜单成功",sysMenuService.toAssignByRoleId(roleId));
        } catch (Exception ex) {
            log.info("根据角色获取菜单失败",ex);
            return WebResponse.resFail("根据角色获取菜单失败",null);
        }
    }

    @OperLog(title = "给角色分配菜单", businessType = BusinessType.OTHER)
    @ApiOperation("给角色分配菜单")
    @PostMapping("doAssign")
    public WebResponse doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        try {
            return WebResponse.resSuccess("给角色分配菜单成功",sysMenuService.doAssignByRoleId(assginMenuVo));
        } catch (Exception ex) {
            log.info("根据角色获取菜单失败",ex);
            return WebResponse.resFail("给角色分配菜单失败",null);
        }
    }


}
